package com.ecnu.jiagou.damai.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.SimpleTimeZone;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.ecnu.jiagou.damai.entity.User;
import com.ecnu.jiagou.damai.result.CodeMsg;
import com.ecnu.jiagou.damai.result.Result;
import com.ecnu.jiagou.damai.service.UserService;
import com.ecnu.jiagou.damai.utils.MD5Util;
import com.ecnu.jiagou.damai.utils.UUIDUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    
    private static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result<User> users(HttpServletRequest request){
        String userid = request.getParameter("userid");
        String password = request.getParameter("password");
        password = MD5Util.inputPassToFormPass(password);
        
        return userService.getUserById(Long.parseLong(userid),password);
    }

    @PostMapping("/loginOut")
    public Result<String> out(HttpSession session){
        session.invalidate();
        return Result.success("success");
    }

    @GetMapping("/ready")
    public Result<String> ready(HttpSession session,HttpServletRequest request){
        session.invalidate();
        HttpSession new_session = request.getSession();
        String uuid = UUIDUtil.uuid();
        String uuid_md5 = MD5Util.inputPassToFormPass(uuid);
        log.info(uuid);
        log.info(uuid_md5);
        new_session.setAttribute("registerId", uuid_md5);

        return Result.success(uuid);
    }

    @PostMapping("/register")
    public Result<Map<String,String>> register(User user, String valid, HttpSession session){
        String uuid = session.getAttribute("registerId").toString();
        session.invalidate();
        if(!uuid.equals(valid)){
            return Result.error(CodeMsg.ERROR);
        }
        if(userService.getUserByName(user.getUsername()) != null){
            return Result.error(CodeMsg.REPEAT);
        }
        user.setRegister(new Date());
        user.setPassword(MD5Util.inputPassToDbPass(user.getPassword(), "1a2b3c4d"));
        return userService.addUser(user);
    }

    @GetMapping("/getInfo")
    public Result<User> getAccountInfo(Long userid){
        User user = userService.getAccountById(userid);
        user.setPassword(null);
        user.setRegister(null);
        return Result.success(user);
    }

    @PostMapping("/updateInfo")
    public Result<Boolean> updateAccountInfo(Long userid,String username,String name,String gender,String birth) throws ParseException {
        if(userid == null){
            return Result.error(CodeMsg.LOST);
        }
        boolean flag = false;
        User user = new User();
        user.setUserid(userid);
        user.setUsername(username);
        user.setName(name);
        user.setGender(gender.charAt(0));
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birth);
        user.setBirth(date);
        flag = userService.updateInfo(user);
        return Result.success(flag);
    }
}
